package com.gugee.tiktok.common.model.dal.ranking.impl;

import com.gugee.tiktok.common.model.dal.ranking.RankingFollowersExtRepository;
import com.gugee.tiktok.common.model.ranking.RankingFollowers;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class RankingFollowersExtRepositoryImpl implements RankingFollowersExtRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void bathSave(List<RankingFollowers> rankingFollowers) {
        long seconds = DateTimeUtils.currentTimeSecond();

        jdbcTemplate.batchUpdate("INSERT INTO " +
                "`tiktok`.`ranking_followers`" +
                "(`blogger_type`, `ranking`, `ranking_type`, `blogger_id`, `uid`," +
                " `nickname`, `unique_id`, `avatar`, `state`, `position`, " +
                "`follower`, `follower_growth`, `belong`, `create_time`, " +
                "`create_by`, `update_time`, `update_by`,`enterprise_verify_reason`,`custom_verify`,`region`,`engagement_rate`," +
                "`with_fusion_shop_entry`,`bio_url`,`bio_email`) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                RankingFollowers followers = rankingFollowers.get(i);
                preparedStatement.setLong(1,followers.getBloggerType());
                preparedStatement.setInt(2,followers.getRanking());
                preparedStatement.setInt(3,followers.getRankingType());
                preparedStatement.setLong(4,followers.getBloggerId());
                preparedStatement.setLong(5,followers.getUid());
                preparedStatement.setString(6,followers.getNickname());
                preparedStatement.setString(7,followers.getUniqueId());
                preparedStatement.setString(8,followers.getAvatar());
                preparedStatement.setString(9,followers.getState());
                preparedStatement.setInt(10,followers.getPosition());
                preparedStatement.setInt(11,followers.getFollower());
                preparedStatement.setInt(12,followers.getFollowerGrowth());
                preparedStatement.setInt(13,followers.getBelong());
                preparedStatement.setLong(14,seconds);
                preparedStatement.setString(15,followers.getCreateBy());
                preparedStatement.setLong(16,seconds);
                preparedStatement.setString(17,followers.getUpdateBy());
                preparedStatement.setString(18,followers.getEnterpriseVerifyReason());
                preparedStatement.setString(19,followers.getCustomVerify());
                preparedStatement.setString(20,followers.getRegion());
                preparedStatement.setDouble(21,followers.getEngagementRate());
                preparedStatement.setBoolean(22,followers.isWithFusionShopEntry());
                preparedStatement.setString(23,followers.getBioUrl());
                preparedStatement.setString(24,followers.getBioEmail());
            }

            @Override
            public int getBatchSize() {
                return rankingFollowers.size();
            }
        });
    }
}
